use strict;
use utf8;

binmode(STDIN, ":encoding(utf-8)");
binmode(STDOUT, ":encoding(utf-8)");

$/ = "<\/page>";
my $d = ".";
$d = $ARGV[0] if ($#ARGV >= 0);

while (<STDIN>) {
  if ($_ =~ /{{(Стадион|Футболист|Хоккеист|Баскетболист|Стадион|Карточка ФК|Хоккейный клуб|Государство|НП-[А-ЯЁа-яё\- ]+|НП)/) {
    my $type = $1;
    $_ =~ /<title>([^<]+)<\/title>/;
    my $title = $1; $title =~ s/[\"\']//g;
    $_ =~ /<id>(\d+)<\/id>/;
    my $id = $1;
    mkdir "$d\/$type" if (! -d "$d\/$type");
    open(F, ">$d\/$type\/$id-$title.page");
    binmode(F, ":encoding(utf-8)");
    print F $_;
    close(F);
    #print "$id\t$title\t$type\n";
  }
}
